const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight:'',
    bannerList: [
      {

      }
    ],
    bannerSubscript:0,
    navList:[],
    navListIndex:[],
    navSubscript:0,
    recommend_tab_index:0,
    indexInfo:'',
    userType:'',
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 500,
    activeList:[],
    token:'',
    swiperIdx:0,
    scrollTop: 0
  },
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  goToVote:function(e) {
    console.log(e.currentTarget.dataset)
    var { name } = e.currentTarget.dataset;
    if(name == '投票') {
      wx.navigateTo({
        url: '/votePage/pages/index/index',
      })
    }
  },

  goNews(e){
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/news/news?id=' + id,
    })
  },

  onPageScroll: function(res) {
    let scrollTop = res.scrollTop;
    this.setData({
      scrollTop
    })
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userType: wx.getStorageSync('userType'),
      token:app.globalData.token,
      navHeight:app.globalData.navHeight
    })
    var _this = this;
   /* wx.getSystemInfo({
      success(res){
        _this.setData({
          navHeight: res.statusBarHeight
        })
      }
    })*/
    var navList = this.data.navList
    let arr = (function (arr, length){
      var navArr = []
      while (arr.length){
        navArr.push(arr.splice(0, length))
      }
      return navArr
    })(navList,10)
    this.setData({
      navListIndex: arr
    })
    wx.request({
      url: app.globalData.baseUrl + 'siteName',
      data: {},
      success(res) {
        if (res.data.code == 1000) {
          _this.setData({
            siteName: res.data.data.site_name
          })
        }
      }
    })

    // 类别列表
    this.indexTypeList();
    // 推荐
    this.getIndexBanner();
    this.getActiveList()
  },

  swiperChange(e) {
    this.setData({
      swiperIdx:e.detail.current
    })
  },

  catchTouchMove(){
    return false
  },

  // 获取banner下标
  navChange: function (e) {
    this.setData({
      navSubscript: e.detail.current
    })
  },
  // 点击推荐tab
  recommendTabClick:function(e){
    var { index } = e.currentTarget.dataset
    this.setData({
      recommend_tab_index:index
    })
  },

  getActiveList(){
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'activelist',
      data: {},
      success(res) {
        console.log(res.data,'res.data.data---00000')
        if (res.data.code == 1000) {
          if (res.data.data != ''){
            _this.setData({
              activeList: res.data.data
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
   // 类别列表
   getIndexBanner:function(){
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'user/slideInfo',
      data: {},
      success(res) {
        if (res.data.code == 1000) {
          if (res.data.data != ''){
            _this.setData({
              bannerList: res.data.data
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 类别列表
  indexTypeList:function(){
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'index/indexCate',
      data: {},
      success(res) {
        if (res.data.code == 1000) {
          _this.setData({
            navList: res.data.data.cateList.splice(0,7)
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 推荐
  indexTaskList:function(){
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'index/getIndexTaskList',
      data: {
        token:app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          _this.setData({
            indexInfo: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  getMore(url){
    wx.navigateTo({url: url.currentTarget.dataset.url})
  },

  // 点击任务
  taskDetail:function(e){
    var { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/taskPage/pages/taskDetail/taskDetail?id=' + id,
    })
  },
  // 点击商城列表
  shopList: function (e) {
    var { id,type } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/taskPage/pages/worksDetail/worksDetail?id=' + id + '&type=' + type,
    })
  },
  // 搜索
  goSearch:function(){
    wx.navigateTo({
      url: '/pages/searchPage/searchPage',
    })
  },
  // 分类列表
  navType:function(e){
    var { typeid, typetext,index } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/taskPage/pages/worksList/worksList?id=' + typeid + '&text=' + typetext+'&index='+index,
    })
  },

  // 点击服务列表
  talentListBtn: function (e) {
    var { id, uid, shop_status } = e.currentTarget.dataset;

    if(id>0){
       //店铺
      var shopType = 1;
    }else{
     //空间
      id = uid;
      var shopType = 2;
    }

    //console.log('/pages/talentDetail/talentDetail?id=' + id + '&shopType=' + shopType);
    wx.navigateTo({
      url: '/taskPage/pages/talentDetail/talentDetail?id=' + id + '&shopType=' + shopType,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      indexInfo:"",
    })
    this.indexTaskList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
